package org.dromara.system.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.PageData;
import org.dromara.common.core.domain.R;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseService;
import org.dromara.system.domain.SysDictData;
import org.dromara.system.service.impl.SysDictDataServiceImpl;
import org.dromara.system.service.impl.SysDictTypeServiceImpl;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 数据字典信息
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/dict/data")
public class SysDictDataController extends BaseService {

  private final SysDictDataServiceImpl dictDataService;
  private final SysDictTypeServiceImpl dictTypeService;

  /**
   * 查询字典数据列表
   */
  @SaCheckPermission("system:dict:list")
  @GetMapping("/list")
  public TableDataInfo<SysDictData> list(SysDictData dictData, PageQuery pageQuery) {
    System.out.println(dictData.toString());
    return dictDataService.selectPageDictDataList(dictData, pageQuery);
  }

  /**
   * 导出字典数据列表
   */
  @Log(title = "字典数据", businessType = BusinessType.EXPORT)
  @SaCheckPermission("system:dict:export")
  @PostMapping("/export")
  public void export(SysDictData dictData, HttpServletResponse response) {
    List<SysDictData> list = dictDataService.selectDictDataList(dictData);
    ExcelUtil.exportExcel(list, "字典数据", SysDictData.class, response);
  }

  /**
   * 查询字典数据详细
   *
   * @param dictId 字典code
   */
  @SaCheckPermission("system:dict:query")
  @GetMapping(value = "/{dictId}")
  public R<SysDictData> getInfo(@PathVariable Long dictId) {
    return R.ok(dictDataService.selectDictDataById(dictId));
  }

  /**
   * 根据字典类型查询字典数据信息
   *
   * @param dictType 字典类型
   */
  @GetMapping(value = "/type/{dictType}")
  public PageData dictType(@PathVariable String dictType) {
    return this.resData(dictTypeService.selectDictDataByType(dictType));
  }

  /**
   * 新增字典类型
   */
  @SaCheckPermission("system:dict:add")
  @Log(title = "字典数据", businessType = BusinessType.INSERT)
  @PostMapping
  public PageData add(@Validated @RequestBody SysDictData dict) {
    return this.resData(dictDataService.insertDictData(dict));
  }

  /**
   * 修改保存字典类型
   */
  @SaCheckPermission("system:dict:edit")
  @Log(title = "字典数据", businessType = BusinessType.UPDATE)
  @PutMapping
  public PageData edit(@Validated @RequestBody SysDictData dict) {
    return this.resData(dictDataService.updateDictData(dict));
  }

  /**
   * 删除字典类型
   *
   * @param data_id 字典code串
   */
  @SaCheckPermission("system:dict:remove")
  @Log(title = "字典类型", businessType = BusinessType.DELETE)
  @DeleteMapping("/{data_id}")
  public R<Void> remove(@PathVariable Long[] data_id) {
    dictDataService.deleteDictDataByIds(data_id);
    return R.ok();
  }
}
